package com.telit.firecommand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telit.firecommand.dao.CadPlotActionDao;
import com.telit.firecommand.entity.CadPlotAction;
import com.telit.firecommand.service.ICadPlotActionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 标绘操作 服务实现类
 * </p>
 *
 * @author yummy
 * @since 2021-07-31
 */
@Service
@Transactional
public class CadPlotActionServiceImpl extends ServiceImpl<CadPlotActionDao, CadPlotAction> implements
        ICadPlotActionService {

    @Override
    public int add(CadPlotAction cadPlotAction) {
        return baseMapper.insert(cadPlotAction);
    }

    @Override
    public int delete(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<Integer> deleteBatch(Integer[] ids) {
        List<Integer> values = new ArrayList<Integer>();
        if (ids != null && ids.length > 0) {
            for (Integer id : ids) {
                values.add(delete(id));
            }
        }
        return values;
    }

    @Override
    public int updateData(CadPlotAction cadPlotAction) {
        return baseMapper.updateById(cadPlotAction);
    }

    @Override
    public CadPlotAction findById(Integer id) {
        return baseMapper.selectById(id);
    }


    @Override
    public IPage<CadPlotAction> findListByPage(CadPlotAction cadPlotAction, Integer page, Integer pageCount) {
        IPage<CadPlotAction> cadPlotActionPage = new Page<>(page, pageCount);
        QueryWrapper<CadPlotAction> wrapper = new QueryWrapper<>();
        if (cadPlotAction != null) {
            if (StringUtils.hasText(cadPlotAction.getName())) {
                wrapper.eq("name", cadPlotAction.getName());
            }
            if (StringUtils.hasText(cadPlotAction.getValue())) {
                wrapper.eq("value", cadPlotAction.getValue());
            }
        }
        return baseMapper.selectPage(cadPlotActionPage, wrapper);
    }

    @Override
    public List<CadPlotAction> searchNoPage(CadPlotAction cadPlotAction) {
        QueryWrapper<CadPlotAction> wrapper = new QueryWrapper<>();
        if (cadPlotAction != null) {
            if (StringUtils.hasText(cadPlotAction.getName())) {
                wrapper.eq("name", cadPlotAction.getName());
            }
            if (StringUtils.hasText(cadPlotAction.getValue())) {
                wrapper.eq("value", cadPlotAction.getValue());
            }
        }
        return baseMapper.selectList(wrapper);
    }

}
